<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Goods;

use app\common\model\Category;
use fast\Tree;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $where = [
            'flag'   => ['like', '%index%'],
            'status' => 3,
        ];
        $goodsList = Goods::with('group')
            ->where($where)
            ->order('weigh', 'desc')
            ->select();
            $a = db::getLastSql();
        $this->success('返回成功', ['banner' => config('site.banner'), 'goodsList' => $goodsList]);
    }


    public function aaa()
    {
        $this->model = model('app\common\model\Category');

        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
// dump($tree->arr);
// dump($tree->getTreeArray(0));
        $this->arr = db('category')->select();
        // dump(db('category')->select());
        // dump(Category::select());
        // dump(model('app\common\model\Category')->select());
        // dump($this->model->order('weigh desc,id desc')->select());
        // dump(collection($this->model->order('weigh desc,id desc')->select()));
        // dump($this->arr);
        $a = $tree->getTreeList($tree->getTreeArray(0), 'name');
        // $a  = $this->getTreeArray(0);
        dump($a);
die;
        $a = $tree->getTreeList($tree->getTreeArray(0), 'name');
    }

    public $pidname = 'pid';
    public $arr = [

    ];
    /**
     *
     * 获取树状数组
     * @param string $myid       要查询的ID
     * @param string $itemprefix 前缀
     * @return array
     */
    public function getTreeArray($myid)
    {

        $this->pidname = 'pid';
        $childs = [];
        foreach ($this->arr as $value) {
            if (!isset($value['id'])) {
                continue;
            }
            if ($value[$this->pidname] == $myid) {
                $childs[$value['id']] = $value;
            }
        }
        $n = 0;
        $data = [];
        $number = 1;
        if ($childs) {
            $total = count($childs);
            foreach ($childs as $id => $value) {
                $data[$n] = $value;
                $data[$n]['childlist'] = $this->getTreeArray($id);
                $n++;
                $number++;
            }
        }
        return $data;
    }

    function aa()
    {
        dump($this->auth->getUserinfo());
        dump($this->auth->getUser());
    }
}
